// pages/order/orderInfo/orderInfo.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderInfo:{},
    timer:null,
    totalPrice: ''
  },
  getOrderInfo:function(ordersns){
    clearInterval(this.data.timer);
    let ordersn=ordersns || this.data.orderInfo.ordersn;
    let data={token:app.globalData.token,ordersn}
    app.requestJs('Order/orderdetail',{data}).then((res)=>{
      console.log(res)
      // return false;
      if(res.errcode==1){
        let resData=res.data || {};
        resData.orderstatus_1=resData.orderstatus.includes(1);
        resData.orderstatus_2=resData.orderstatus.includes(2);
        resData.orderstatus_3=resData.orderstatus.includes(3);
        resData.orderstatus_4=resData.orderstatus.includes(4);
        resData.orderstatus_5=resData.orderstatus.includes(5);
        resData.orderstatus_6=resData.orderstatus.includes(6);
        resData.orderstatus_7=resData.orderstatus.includes(7);
        resData.orderstatus_8=resData.orderstatus.includes(8);
        resData.orderstatus_9=resData.orderstatus.includes(9);
        resData.orderstatus_10=resData.orderstatus.includes(10);
        let price = ''
        if(res.data.shipping_price != '免运费') {
          price = res.data.shipping_price.split('￥')[1]
          price = Number(price) + Number(res.data.order_amount)
        } else {
          price = Number(res.data.order_amount)
        }
        this.setData({
          orderInfo:resData,
          totalPrice: price
        })
        if(resData.countdown>0)this.setIntervalApi(resData.countdown);
      }else app.utilJs.toast({ title: res.message });
    })
  },
  orderbind_before:function(e){
    let that=this;
    let obj=this.data.orderInfo,
        orderid=obj.orderid,
        orderstatus=e.currentTarget.dataset.typeid;
        console.log(e);
        console.log(obj);
    if(orderstatus==7)wx.navigateTo({url: '/pages/order/express_info/express_info?orderid='+orderid});
    else if(orderstatus==4){
      // that.getOrderInfo();
      wx.navigateTo({
        url: '/pages/order/refund_info/refund_info?orderid='+orderid,
        events:{
          updateData:function(){
            that.getOrderInfo();
          }
        }
      })
    }
    else {
      let data={orderid,orderstatus,token:app.globalData.token};
      if(data.orderstatus==10)data.ordersn=obj.ordersn;
      let msgContent='';
      if(data.orderstatus==3)msgContent="确定要取消订单吗";
      else if(data.orderstatus==6)msgContent="确定要确认收货吗";
      else if(data.orderstatus==9)msgContent="确定要删除订单吗";
      if (msgContent=='') {
        that.orderStateBtn(data);
      } else {
        that.orderHandleModal(msgContent,data);
      }
    }
  },
  orderHandleModal:function(msgContent,data){//提示
    let that=this;
    wx.showModal({
      title: '提示',
      content:msgContent,
      success (res) {
        if (res.confirm) {
          that.orderStateBtn(data);
        }
      }
    })
  },
  orderStateBtn:function(data){//订单聚合操作
    if(data.orderstatus==10)data.ordersn=this.data.orderInfo.ordersn;
    app.requestJs("order/orderjuhehander",{data}).then((res) => {
      if (res.errcode == 1) {
        app.utilJs.toast({ title: res.message,icon:'success'});
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.emit('updateData', {data: 'test'});
        this.getOrderInfo();
      } else {
        app.utilJs.toast({ title: res.message });
      }
    })
  },
  get_wxpayApi:function(e){
    let ordersn=this.data.orderInfo.ordersn;
    let data={token:app.globalData.token,ordersn};
    app.requestJs('Payment/getpay',{data}).then((res) => {
      if (res.errcode == 1) {
        this.order_wxPay(JSON.parse(res.data),ordersn);
      } else {
        app.utilJs.toast({ title: res.message });
      }
    })
  },
  order_wxPay:function(data,ordersn){//调起支付
    console.log(data);
    console.log(ordersn)
    let that=this;
    wx.requestPayment({
      timeStamp:data.timeStamp,
      nonceStr:data.nonceStr,
      package:data.package,
      signType:data.signType,
      paySign:data.paySign,
      success(res) {
        console.log(res);
        if(res.errMsg=='requestPayment:ok'){
          that.getOrderInfo();
          const eventChannel = that.getOpenerEventChannel()
          eventChannel.emit('updateData', {data: 'test'});
          setTimeout(()=>{
            app.utilJs.toast({ title:"支付成功" ,icon:'success'});
          },200)
        }
      },
      fail(error) {
        console.log(error)
        app.utilJs.toast({ title: '支付失败' });
      }
    })
  },
  orderInfoLink:function(e){
    let that=this;
    let orderid=this.data.orderInfo.orderid;
    wx.navigateTo({
      url: '/pages/order/refund_info/refund_info?orderid='+orderid,
      events:{
        updateData:function(){
          that.getOrderInfo();
        }
      }
    })
  },
  copyBind:function(){
    let data = this.data.orderInfo.ordersn;
    console.log(data);
    if(data=='' || !data)app.utilJs.toast({title:'没有需要复制的内容!'})
    else{
      wx.setClipboardData({
        data,
        success(res) {
          wx.getClipboardData({
            success(res) {
              wx.showToast({
                title: '复制成功',
              })
            }
          })
        }
      })
    }
  },
  setIntervalApi:function(time){//计时器
    if(time<=0){
      clearInterval(this.data.timer);
      return
    }
    let timer=this.data.timer;
    let outTime='';
    timer=setInterval(()=>{
      console.log(this.data.timer);
      if(time==0){
        clearInterval(this.data.timer);
        this.getOrderInfo();
      }else if(!this.data.timer)clearInterval(this.data.timer);
      else time=time-1;
      let hTime=time;
      console.log(time);
      // let d=Math.floor(time/86400)>9?Math.floor(time/86400):'0'+Math.floor(time/86400);
      // let hTime=time%86400;
      let h=Math.floor(hTime/3600)>9?Math.floor(hTime/3600):'0'+Math.floor(hTime/3600);
      let mTime=hTime%3600;
      let m=Math.floor(mTime/60)>9?Math.floor(mTime/60):'0'+Math.floor(mTime/60);
      let s=mTime%60>9?mTime%60:'0'+mTime%60;
      outTime={h,m,s};
      console.log(outTime)
      this.setData({
        outTime
      })
    },1000);
    this.setData({
      timer
    })
  },
  anotherOrder () {
    const data = {
      token: app.globalData.token,
      ordersn: this.data.orderInfo.ordersn
    }
    app.requestJs("Order/another_order",{data: data, status1: true}).then(() => {
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/cart/cart'
        })
      }, 1000)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let ordersn=options.ordersn || '';
    // 测试用
    // let data = {
    //   consignee: '张三丰',
    //   mobile: '13758493756',
    //   address: '星尚发展大厦写字楼A座411室',
    //   ordersn: '297537693576945764',
    //   statusstr: '待发货',
    //   order_goods: [
    //     {
    //       id: 1,
    //       thumb: '/img/61.png',
    //       goods_name: '海南小台农芒果',
    //       specname: '100g',
    //       price: '19.90',
    //       goods_num: 1
    //     },{
    //       id: 2,
    //       thumb: '/img/61.png',
    //       goods_name: '海南贵妃芒果当季热带新鲜水果',
    //       specname: '100g',
    //       price: '19.90',
    //       goods_num: 1
    //     }
    //   ],
    //   add_time: '2024.05.28 12:43:25',
    //   goods_num: 2,
    //   user_note: '老板帮我早点发货吧，包装弄好点，快递小哥不要帮我暴力运输啊好多文字好多文字展示',
    //   order_amount: '39.80',
    //   orderstatus_10: true,
    //   countdown: 1000
    // }
    // if(data.countdown>0)this.setIntervalApi(data.countdown);
    // this.setData({
    //   orderInfo: data
    // })
    // return
    this.getOrderInfo(ordersn);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      safeAreaB:app.globalData.safeArea
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})